package service;

import entity.TestsPaper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import view.ExamRules;

import java.io.*;
import java.util.List;

/**
 * 试题操作
 */
public class TestsOpt {
    private static String testsPath = "D:\\Tests.xlsx";
    private static String rulePath = "D:\\Examrule.txt";
    private static Workbook wb;
    private static Sheet sheet;
    static {
        try {
            wb = getWorkBook(testsPath,new FileInputStream(new File(testsPath)));
            sheet = getSheetByIndex(wb,0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 根据下标获取工作表
     * @param wb
     * @param index
     * @return
     */
    public static Sheet getSheetByIndex(Workbook wb, int index){
        return wb.getSheetAt(index);
    }
    /**
     * 获取sheet表的总数
     * @param wb
     * @return
     */
    public static int getSumSheet(Workbook wb){
        return wb.getNumberOfSheets();
    }
    /**
     * 获取试题总数
     * @return
     * @throws IOException
     */
    public static int getTotalSum() throws IOException {
        return sheet.getPhysicalNumberOfRows();
    }
    /**
     *
     * @param path: 文件路径
     * @param in: 指向文件的输入流
     * @return  :WorkBook：poi中的一个类，提供了获取到Excel文件内容的相关方法
     * @throws IOException
     */
    public static Workbook getWorkBook(String path, InputStream in) throws IOException {
        Workbook workbook = null;
        // 判断文件后缀名是否为xls
        boolean isExcel2003 = path.matches("^.+(?i)(xls)$");
        if (isExcel2003){                     // 当前文件名后缀为xls，说明为2003之前的版本
            workbook = new HSSFWorkbook(in);
        }else {
            workbook = new XSSFWorkbook(in);
        }
        return workbook;
    }
    /**
     * 根据试题行获取一道试题
     * @param row
     * @return
     */
    public static TestsPaper getTestByRow(int row){
        Row row1 = sheet.getRow(row);
        TestsPaper test = new TestsPaper();
        test.setRow(row);
        test.setStem(row1.getCell(1).getStringCellValue());
        test.setTestsType(row1.getCell(0).getStringCellValue());
        test.setOptions(row1.getCell(2).getStringCellValue());
        test.setCorrectOpt(row1.getCell(3).getStringCellValue());
        return test;
    }

    /**
     * 返回考试准则界面
     * @return
     */
    public static ExamRules getExamRules() throws FileNotFoundException {
        if (getRules() != null){
            return new ExamRules(getRules());
        }
        return new ExamRules("考试准测加载异常");
    }

    /**
     * 以字符串形式返回考试准则
     * @return
     * @throws FileNotFoundException
     */
    public static String getRules() throws FileNotFoundException {
        File file = new File(rulePath);
        StringBuilder result = new StringBuilder();
        if (file.isFile() && file.exists()){          // 判断文件是否存在
            InputStreamReader read = new InputStreamReader(new FileInputStream(file));
            BufferedReader bufferedReader = new BufferedReader(read);
            String line = "";
            try {
                while ((line = bufferedReader.readLine()) != null){
                    result.append(line+"\n");
                }
                read.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return String.valueOf(result);
    }

}
